
****FIRST FIVE ROWS***
est clear
use "$jfqa_rep/20_Stock_Setup.dta", clear

qui{
foreach i in ibes_est_12 futret convexity_split {
	drop if missing(`i')
}

drop if year<1983
drop if year==1983 & month<12

drop if year>2022
drop if year==2022 & month==12

*convexity split rank
sort monthyear convexity_split
gen convex_five=.
by monthyear: replace convex_five=1 if _n<_N/5 
by monthyear: replace convex_five=2 if _n>=_N/5 & _n<_N/5*2 
by monthyear: replace convex_five=3 if _n>=_N/5*2 & _n<_N/5*3 
by monthyear: replace convex_five=4 if _n>=_N/5*3 & _n<_N/5*4 
by monthyear: replace convex_five=5 if _n>=_N/5*4

*ibes_est_12ity rank
sort monthyear ibes_est_12
gen ibes_est_12_rank=. 
by monthyear: replace ibes_est_12_rank=1 if _n<_N/5 
by monthyear: replace ibes_est_12_rank=2 if _n>=_N/5 & _n<_N/5*2 
by monthyear: replace ibes_est_12_rank=3 if _n>=_N/5*2 & _n<_N/5*3 
by monthyear: replace ibes_est_12_rank=4 if _n>=_N/5*3 & _n<_N/5*4 
by monthyear: replace ibes_est_12_rank=5 if _n>=_N/5*4

replace futret=futret*100

egen convex_liquid=group(convex_five ibes_est_12_rank)

egen vw_raw=wtmean(futret), weight(mktcap) by(monthyear convex_liquid)

*consolidate to one observation per month
sort convex_liquid monthyear 
collapse vw_raw, by(convex_liquid monthyear year month)

tsset convex_liquid monthyear  

forval i=1/25 {
	eststo: newey vw_raw if convex_liquid ==`i', lag(6)
}
}

****LAST ROW***
use "$jfqa_rep/20_Stock_Setup.dta", clear

qui{
foreach i in ibes_est_12 futret convexity_split {
	drop if missing(`i')
}

drop if year<1983
drop if year==1983 & month<12

drop if year>2022
drop if year==2022 & month==12

*convexity split rank
sort monthyear convexity_split
gen convex_five=.
by monthyear: replace convex_five=1 if _n<_N/5 
by monthyear: replace convex_five=2 if _n>=_N/5 & _n<_N/5*2 
by monthyear: replace convex_five=3 if _n>=_N/5*2 & _n<_N/5*3 
by monthyear: replace convex_five=4 if _n>=_N/5*3 & _n<_N/5*4 
by monthyear: replace convex_five=5 if _n>=_N/5*4

*ibes_est_12ity rank
sort monthyear ibes_est_12
gen ibes_est_12_rank=. 
by monthyear: replace ibes_est_12_rank=1 if _n<_N/5 
by monthyear: replace ibes_est_12_rank=2 if _n>=_N/5 & _n<_N/5*2 
by monthyear: replace ibes_est_12_rank=3 if _n>=_N/5*2 & _n<_N/5*3 
by monthyear: replace ibes_est_12_rank=4 if _n>=_N/5*3 & _n<_N/5*4 
by monthyear: replace ibes_est_12_rank=5 if _n>=_N/5*4

replace futret=futret*100

egen convex_liquid=group(convex_five ibes_est_12_rank)

egen vw_raw=wtmean(futret), weight(mktcap) by(monthyear convex_liquid)

*consolidate to one observation per month
sort convex_liquid monthyear 
collapse vw_raw, by(convex_liquid monthyear year month)

keep if convex_liquid==1 | convex_liquid==2 | convex_liquid==3 | convex_liquid==4 | convex_liquid==5 | convex_liquid==21 | convex_liquid==22 | convex_liquid==23 | convex_liquid==24 | convex_liquid==25

sort monthyear convex_liquid

gen vw_raw_dif=vw_raw-vw_raw[_n+5] if convex_liquid==1 | convex_liquid==2 | convex_liquid==3 | convex_liquid==4 | convex_liquid==5

keep if convex_liquid==1 | convex_liquid==2 | convex_liquid==3 | convex_liquid==4 | convex_liquid==5

sort convex_liquid monthyear
tsset convex_liquid monthyear  

foreach i in 1 2 3 4 5 {
eststo: newey vw_raw_dif  if convex_liquid ==`i', lag(6)
}
}

esttab using "$jfqa_rep/Table_8C.csv", nostar replace  b(2)
